﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class merchant_levelDal
    {
        /// <summary>
        /// 添加VIP规则
        /// </summary>
        /// <param name="merchantid"></param>
        /// <param name="amount"></param>
        /// <param name="levelnum"></param>
        /// <returns></returns>
        public static int Addlevelreul(string merchantid, string amount, string levelnum, string discount_rate)
        {
            string sql = "insert into ms_merchant_level(lv_num,merchant_id,amount,update_time,discount_rate) values(@lv_num,@merchant_id,@amount,@update_time,@discount_rate)";
            MySqlParameter[] values = new MySqlParameter[5];
            values[0] = new MySqlParameter("lv_num", levelnum);
            values[1] = new MySqlParameter("merchant_id", merchantid);
            values[2] = new MySqlParameter("amount", amount);
            values[3] = new MySqlParameter("update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            values[4] = new MySqlParameter("discount_rate", discount_rate);
            return DBHelper.ExcutUpdate(sql, values);
        }
        /// <summary>
        /// 查商户等级规则
        /// </summary>
        /// <param name="merchantid"></param>
        /// <returns></returns>
        public static List<ms_merchant_level> Querybymerid(string merchantid)
        {
            string sql = "select * from ms_merchant_level where merchant_id=@merchant_id";
            MySqlParameter[] values = new MySqlParameter[1];
            values[0] = new MySqlParameter("merchant_id", merchantid);
            List<ms_merchant_level> levelist = new List<ms_merchant_level>();
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            while (rd.Read())
            {
                ms_merchant_level level = new ms_merchant_level();
                level.id = rd["id"].ToString();
                level.merchant_id = rd["merchant_id"].ToString();
                level.lv_num = rd["lv_num"].ToString();
                level.amount = Convert.ToInt32(rd["amount"].ToString());
                level.update_time = rd["update_time"].ToString();
                level.discount_rate =Convert.ToInt32( rd["discount_rate"].ToString());
                levelist.Add(level);
            }
            rd.Close();
            return levelist;

        }
        /// <summary>
        /// 通过该商户等级查是否存在
        /// </summary>
        /// <param name="merchantid"></param>
        /// <param name="lvname"></param>
        /// <returns></returns>
        public static int Querybymerchantid(string merchantid, string lvname)
        {
            string sql = "select count(*) as count from ms_merchant_level where lv_num=@lv_num and merchant_id=@merchant_id";
            MySqlParameter[] values = new MySqlParameter[0];
            values[0] = new MySqlParameter("lv_num", lvname);
            values[1] = new MySqlParameter("merchant_id", merchantid);
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            int count = 0;
            if (rd.Read())
            {
                if (!string.IsNullOrEmpty(rd["count"].ToString()))
                    count = Convert.ToInt32(rd["count"]);
            }
            rd.Close();
            return count;
        }
        /// <summary>
        /// 更新等级规则
        /// </summary>
        /// <param name="levelname"></param>
        /// <param name="merchantid"></param>
        /// <param name="amount"></param>
        /// <returns></returns>
        public static int Updateamountbylevelid(string levelname, string levelid,string amount,string discount_rate)
        {
            string sql = "update ms_merchant_level set amount=@amount,update_time=@update_time,discount_rate=@discount_rate where lv_num=@lv_num and id=@levelid ";
            MySqlParameter[] values = new MySqlParameter[5];
            values[0] = new MySqlParameter("lv_num", levelname);
            values[1] = new MySqlParameter("levelid", levelid);
            values[2] = new MySqlParameter("update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            values[3] = new MySqlParameter("amount", amount);
            values[4] = new MySqlParameter("discount_rate", discount_rate);
            return DBHelper.ExcutUpdate(sql, values);
        }
        /// <summary>
        /// 删除用户等级规则
        /// </summary>
        /// <param name="merchantid"></param>
        /// <returns></returns>
        public static int Dellevel(string merchantid)
        {
            string sql = "delete from ms_merchant_level where merchant_id=@merchant_id";
            MySqlParameter[] values = new MySqlParameter[1];
            values[0] = new MySqlParameter("merchant_id", merchantid);
            List<ms_merchant_level> levelist = new List<ms_merchant_level>();
            return DBHelper.ExcutUpdate(sql, values);
        }
        /// <summary>
        /// 查会员等级折扣
        /// </summary>
        /// <param name="merchantid"></param>
        /// <param name="money"></param>
        /// <returns></returns>
        public static ms_merchant_level Querybymerchantiduid(string merchantid, string money)
        {
            string sql = "select * from ms_merchant_level where @money>=amount and merchant_id=@merchant_id ORDER BY id desc limit 1";
            MySqlParameter[] values = new MySqlParameter[2];
            values[0] = new MySqlParameter("merchant_id", merchantid);
            values[1] = new MySqlParameter("money", money);
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            ms_merchant_level level = new ms_merchant_level();
            if (rd.Read())
            {
                level.lv = rd["lv"].ToString();
                level.merchant_id = rd["merchant_id"].ToString();
                level.discount_rate = Convert.ToInt32(rd["discount_rate"].ToString());
                level.amount = Convert.ToInt32(rd["amount"].ToString());

            }
            rd.Close();
            return level;
        }
    }
}
